package com.railway.news.util;
import java.awt.Insets;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;

import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;


public class PDFContentHelper {
	
	public static void main(String[] args) throws Exception {
		PDFContentHelper.generatePDF_2(new File("c:/1.pdf"), "c:/1.html");
		 StringReader strReader = new StringReader("<html><head></head></html>");
		 
		 PDFContentHelper.generatePDF_1(new File("c:/1.pdf"), strReader);
	}
	
	// HTML代码来自于HTML文件
	public static void generatePDF_2(File outputPDFFile, String inputHTMLFileName)
			throws Exception {
		FileOutputStream fos = new FileOutputStream(outputPDFFile);
		PD4ML pd4ml = new PD4ML();
		pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
		pd4ml.setHtmlWidth(950);
		pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
		pd4ml.useTTF("java:fonts", true);
		pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");
		pd4ml.enableDebugInfo();
		pd4ml.render("file:" + inputHTMLFileName, fos);
	}
    public static void generatePDF_1(File outputPDFFile, StringReader strReader) throws Exception {  
        FileOutputStream fos = new FileOutputStream(outputPDFFile);  
        PD4ML pd4ml = new PD4ML();  
        pd4ml.setPageInsets(new Insets(20, 10, 10, 10));  
        pd4ml.setHtmlWidth(950);  
        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));  
        pd4ml.useTTF("java:fonts", true);  
        pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");  
        pd4ml.enableDebugInfo();  
        pd4ml.render(strReader, fos);  
    } 
}